package com.github.appintro.indicator;

import com.github.appintro.ResourceTable;
import com.github.appintro.utile.ResUtil;
import com.github.appintro.utile.ShapeUtil;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.utils.LayoutAlignment;
import ohos.app.Context;

public class DotIndicatorController extends DirectionalLayout implements IndicatorController {
    private int selectedIndicatorColor;
    private int unselectedIndicatorColor;
    private int currentPosition;
    private int slideCount;
    private Context context;

    public DotIndicatorController(Context context) {
        this(context, (AttrSet) null);
        selectedIndicatorColor = ResourceTable.Color_appintro_default_selected_color;
        unselectedIndicatorColor = ResourceTable.Color_appintro_default_unselected_color;
    }

    public DotIndicatorController(Context context, AttrSet attrSet) {
        super(context, attrSet);
        this.context = context;
    }

    public int getSelectedIndicatorColor() {
        return this.selectedIndicatorColor;
    }

    public void setSelectedIndicatorColor(int value) {
        this.selectedIndicatorColor = value;
        this.selectPosition(this.currentPosition);
    }

    public int getUnselectedIndicatorColor() {
        return this.unselectedIndicatorColor;
    }

    public void setUnselectedIndicatorColor(int value) {
        this.unselectedIndicatorColor = value;
        this.selectPosition(this.currentPosition);
    }

    @Override
    public Component newInstance(Context context) {
        LayoutConfig newLayoutParams = new LayoutConfig(LayoutConfig.MATCH_CONTENT, LayoutConfig.MATCH_CONTENT);
        newLayoutParams.alignment = LayoutAlignment.VERTICAL_CENTER;
        setLayoutConfig(newLayoutParams);
        setOrientation(HORIZONTAL);
        setAlignment(LayoutAlignment.CENTER);
        return this;
    }

    @Override
    public void initialize(int slideCount) {
        this.slideCount = slideCount;
        for (int i = 0; i < slideCount; i++) {
            Component dot = new Component(this.context);
            dot.setBackground(ShapeUtil.createCircleDrawable(ResUtil.getColor(getContext(), unselectedIndicatorColor), 25));
            DirectionalLayout.LayoutConfig params = new DirectionalLayout.LayoutConfig(
                    30,
                    30);
            params.setMargins(8, 8, 8, 8);
            dot.setLayoutConfig(params);
            if (slideCount == 1) {
                dot.setVisibility(Component.INVISIBLE);
            }
            addComponent(dot, params);
        }
        selectPosition(0);
    }

    @Override
    public void selectPosition(int index) {
        currentPosition = index;
        int tint;
        for (int i = 0; i < slideCount; i++) {
            if (i == index) {
                tint = selectedIndicatorColor;
            } else {
                tint = unselectedIndicatorColor;
            }
            Component imageView = getComponentAt(i);
            imageView.setBackground(ShapeUtil.createCircleDrawable(ResUtil.getColor(getContext(), tint), 25));
        }
    }
}